package com.roads.site.service.impl;

import com.roads.common.config.MasterConfig;
import com.roads.common.constant.Constants;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.file.FileUtils;
import com.roads.common.utils.uuid.IdUtils;
import com.roads.site.domain.SiteLabourStaff;
import com.roads.site.domain.SiteLabourStaffContract;
import com.roads.site.mapper.SiteLabourStaffContractAnnexMapper;
import com.roads.site.mapper.SiteLabourStaffContractMapper;
import com.roads.site.mapper.SiteLabourStaffMapper;
import com.roads.site.service.ISiteLabourStaffContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * 劳务工-合同信息Service业务层处理
 * 
 * @author nhp
 * @date 2023-03-24
 */
@Service
public class SiteLabourStaffContractServiceImpl implements ISiteLabourStaffContractService {

    @Autowired
    private SiteLabourStaffContractMapper mapper;

    @Autowired
    private SiteLabourStaffMapper staffMapper;

    @Autowired
    private SiteLabourStaffContractAnnexMapper contractAnnexMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 劳务工-合同信息主键
     * @return 劳务工-合同信息
     */
    @Override
    public SiteLabourStaffContract getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 根据uuid查询详情
     *
     * @param uuid 劳务工-合同信息uuid
     * @return 劳务工-合同信息
     */
    @Override
    public SiteLabourStaffContract getModelByUuid(String uuid) {
        return mapper.getModelByUuid(uuid);
    }

    /**
     * 查询列表
     * 
     * @param model 劳务工-合同信息
     * @return 劳务工-合同信息
     */
    @Override
    public List<SiteLabourStaffContract> selectModelList(SiteLabourStaffContract model) {
        return mapper.selectModelList(model);
    }

    /**
     * 根据身份证号查询合同信息
     *
     * @param idNo 身份证号
     * @return 劳务工-合同信息集合
     */
    @Override
    public List<SiteLabourStaffContract> selectModelListByIdNo(String idNo) {
        return mapper.selectModelListByIdNo(idNo);
    }

    /**
     * 新增
     * 
     * @param model 劳务工-合同信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SiteLabourStaffContract model) {
        // 查询劳务工所在项目标段
        SiteLabourStaff staff = staffMapper.getModelById(model.getStaffId());
        model.setInstitutionCode(staff.getInstitutionCode());
        String uuid = IdUtils.simpleUuid();
        model.setCreateTime(DateUtils.getNowDate());
        model.setUuid(uuid);
        model.setTop(Constants.YES);
        // 更新其他合同不置顶
        mapper.updateTop(model.getIdNo());
        // 新增的合同默认是置顶
        mapper.insert(model);
        // 同步劳务工表
        staffMapper.syncStaffContract(model.getIdNo(), model.getCode(), model.getStatus(), model.getBeginDate(), model.getEndDate(), model.getSignDate());
        return 1;
    }

    /**
     * 修改
     * 
     * @param model 劳务工-合同信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(SiteLabourStaffContract model) {
        mapper.update(model);
        // 如果是置顶的，需要同步到劳务工表
        if (Constants.YES.equals(model.getTop())) {
            staffMapper.syncStaffContract(model.getIdNo(), model.getCode(), model.getStatus(), model.getBeginDate(), model.getEndDate(), model.getSignDate());
        }
        return 1;
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            delete(ids[i]);
        }
        return 1;
    }

    /**
     * 删除
     * 
     * @param id 劳务工-合同信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) {
        SiteLabourStaffContract contract = mapper.getModelById(id);
        // 判断是否为最新的合同，如果是最新的合同，需要重置劳务工表的合同信息
        if (Constants.YES.equals(contract.getTop())) {
            staffMapper.resetStaffContract(contract.getIdNo());
        }
        // 删除合同附件文件
        String filePath = MasterConfig.getLabourStaffPath() + "/" + contract.getIdNo() + "/contract/" + contract.getUuid();
        FileUtils.deleteDirectory(new File(filePath));
        // 删除合同附件记录
        contractAnnexMapper.deleteByUuid(contract.getUuid());
        // 删除合同信息
        mapper.delete(contract.getId());
        return 1;
    }

    /**
     * 合同附件数量 +1
     *
     * @param uuid uuid
     * @return 结果
     */
    @Override
    public int addFileCount(String uuid) {
        return mapper.addFileCount(uuid);
    }

    /**
     * 合同附件数量 -1
     *
     * @param uuid uuid
     * @return 结果
     */
    @Override
    public int subtractFileCount(String uuid) {
        return mapper.subtractFileCount(uuid);
    }
}
